package hl.demosample.ui.fragment.popwindow

import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.PopupWindow
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import hl.demosample.R
import hl.demosample.app.appViewModel
import hl.demosample.app.base.BaseFragment
import hl.demosample.app.ext.dp2px
import hl.demosample.app.ext.initClose
import hl.demosample.app.ext.setAdapterAnimation
import hl.demosample.app.ext.setUiTheme
import hl.demosample.app.util.ListDataUtil.POP_WINDOW_DATA
import hl.demosample.databinding.FragmentPopwindowBinding
import hl.demosample.ui.adapter.PopWindowAdapter
import kotlinx.android.synthetic.main.include_toolbar.*
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.nav

/*
* path: DemoSample 
* Author: 86133
* Create: 2021-06-02 16:45
* Changes (from 2021-06-02)
* Desc:
*/
class PopWindowFragment:BaseFragment<BaseViewModel,FragmentPopwindowBinding>() {

    private var popupWindow: PopupWindow? = null

    override fun layoutId()= R.layout.fragment_popwindow

    override fun initView(savedInstanceState: Bundle?) {
          toolbar.run {
              initClose("PopWindow"){
                  nav().navigateUp()
              }
              inflateMenu(R.menu.todo_menu)
              setOnMenuItemClickListener{
                  when(it.itemId){
                      R.id.todo_add ->{
                          showAdd()
                      }
                  }
                  true
              }
          }
    }

    override fun createObserver() {
        super.createObserver()
        appViewModel.run {
            //监听全局的主题颜色改变
            appColor.observeInFragment(this@PopWindowFragment) {
                setUiTheme(it, toolbar)
            }
        }
    }

    fun showAdd() {
        //   PopupWindow在Android7.0以上的显示问题（showAsDropDown方法的改动）    ViewGroup.LayoutParams.MATCH_PARENT
//            https://blog.csdn.net/micotale/article/details/82968743?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-0&spm=1001.2101.3001.4242
//            rela_layout  某个控件的View
//            val rect= Rect()
//            rela_layout.getGlobalVisibleRect(rect)
//            val height = UiUtils.getScreenHeidth(this@PopWindowActivity) - rect.bottom
        View.inflate(activity, R.layout.popwindow_top_layout, null).apply {
            val iv = this?.findViewById<ImageView>(R.id.image)
            val recyclerView: RecyclerView? = this?.findViewById(R.id.recyclerView)
            popupWindow = PopupWindow(
                this,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
            popupWindow?.animationStyle = R.style.addPopAnim
            popupWindow?.isFocusable = true
            popupWindow?.isOutsideTouchable = true
            popupWindow?.isTouchable = true
            popupWindow?.showAtLocation(
                toolbar,
                Gravity.TOP or Gravity.END,
                dp2px(context , 14f),
                toolbar.height + dp2px(context, 16f)
            )
            //popupWindow在弹窗的时候背景半透明
            val params = activity?.window?.attributes
            params?.alpha = 0.5f
            activity?.window?.attributes = params
            iv?.setOnClickListener { v: View? -> popupWindow?.dismiss() }
            popupWindow?.setOnDismissListener {
                params?.alpha = 1.0f
                activity?.window?.attributes = params
            }
            recyclerView?.layoutManager = GridLayoutManager(activity, 5)
            val popWindowAdapter = PopWindowAdapter(R.layout.popwindow_item)
            popWindowAdapter.setNewInstance(POP_WINDOW_DATA)
            recyclerView?.adapter = popWindowAdapter
        }
    }
}